/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.events.stat;

import app.models.activity.MallActivityStat;
import app.models.brand.BrandStat;
import app.models.brand.BrandVisit;
import app.models.brand.DailyStat;
import app.models.parking.ParkingRecord;
import com.github.sog.libs.AppFunc;
import com.google.common.eventbus.Subscribe;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import org.joda.time.DateTime;

import java.sql.SQLException;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-06-08 1:18
 * @since JDK 1.6
 */
public class StatBusListener {


    public static final String TIMES  = "times";
    public static final String ACCESS = "access";
    public static final String BRAND  = "brand";

    @Subscribe
    public void brandClick(final BrandClickEvent evt) {
        final long brand_id = evt.brand_id;
        if (brand_id > 0) {
            final DateTime now = DateTime.now();
            final String day = now.toString(AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD);
            Db.tx(new IAtom() {
                @Override
                public boolean run() throws SQLException {
                    DailyStat dailyStat = DailyStat.dao.findByBrandAndDay(brand_id, day);
                    if (dailyStat == null) {
                        dailyStat = new DailyStat();
                        dailyStat.set(BRAND, brand_id).set("count_date", day).set(TIMES, 1).set("create_time", now.toDate());
                        dailyStat.save();
                    } else {
                        dailyStat.set(TIMES, dailyStat.getLong(TIMES) + 1);
                        dailyStat.update();
                    }
                    BrandStat stat = BrandStat.dao.findById(brand_id);
                    if (stat == null) {
                        stat = new BrandStat();
                        stat.set(BRAND, brand_id);
                        stat.set(ACCESS, 1);
                        stat.save();
                    } else {
                        stat.set(ACCESS, stat.getLong(ACCESS) + 1);
                        stat.update();
                    }
                    BrandVisit visit = new BrandVisit();
                    visit.set(BRAND, brand_id);
                    visit.set("visit_time", evt.visit_time);
                    visit.set("client_ip", evt.visit_ip);
                    visit.set("member", evt.member);
                    visit.save();
                    return true;
                }
            });

        }
    }

    @Subscribe
    public void brandActivityClick(final MallActivityClickEvent evt) {
        Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                final long activity = evt.getActivity();
                MallActivityStat stat = MallActivityStat.dao.findById(activity);
                if(stat == null){
                    stat = new MallActivityStat();
                    stat.set("activity", activity).set(TIMES, 1);
                    stat.save();
                } else {
                    stat.set(TIMES, stat.getInt(TIMES) + 1);
                    stat.update();
                }
                return true;
            }
        });
    }

    @Subscribe
    public void parkingClick(final ParkingEvent evt){
        ParkingRecord record = new ParkingRecord();
        record.set("member", evt.member).set("parking_time", evt.date).set("parking_date", evt.date)
                .set("begin_location", evt.beginLocation).set("end_location", evt.endLocation)
        .set("b2", evt.b2);
        record.save();
    }

}
